package co.yixiang.retail.biz.product.service.impl;

import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.dozer.service.IGenerator;


import co.yixiang.retail.biz.product.mapper.YxStoreProductMapper;
import co.yixiang.retail.biz.product.service.YxStoreProductService;
import co.yixiang.retail.common.domain.YxStoreProduct;
import co.yixiang.retail.common.domain.YxSystemStore;
import co.yixiang.retail.common.dto.YxStoreProductDto;
import co.yixiang.retail.common.dto.YxStoreProductQueryCriteria;
import co.yixiang.retail.common.dto.YxSystemStoreDto;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class YxStoreProductServiceImpl extends BaseServiceImpl<YxStoreProductMapper, YxStoreProduct> implements YxStoreProductService {

    @Autowired
    private  IGenerator generator;

    @Autowired
    private  YxStoreProductMapper yxStoreProductMapper;


    @Override
    public Map<String, Object> queryByRetailId(YxStoreProductQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreProduct> page = new PageInfo<>(getProductByRetailList(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxStoreProductDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> queryGoods(YxStoreProductQueryCriteria criteria,Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreProduct> page = new PageInfo<>(this.setNum(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxStoreProductDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    List<YxStoreProduct> setNum(YxStoreProductQueryCriteria criteria){
        List<YxStoreProduct> yxStoreProducts = yxStoreProductMapper.queryGoods(criteria);
        if (!CollectionUtils.isEmpty(yxStoreProducts)){
            yxStoreProducts.forEach((item)->item.setNum(1));
        }
        return yxStoreProducts;
    };

    @Override
    public Map<String, Object> queryBystore(YxStoreProductQueryCriteria criteria, Pageable pageable) {

        getPage(pageable);
        PageInfo<YxStoreProduct> page = new PageInfo<>(yxStoreProductMapper.query(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxSystemStoreDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public YxSystemStore queryRetail(Integer id) {
        return yxStoreProductMapper.queryRetail(id);
    }

    @Override
    public Map<String, Object> queryByCebters(YxStoreProductQueryCriteria criteria,Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreProduct> page = new PageInfo<>(yxStoreProductMapper.queryByCebters(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxStoreProductDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    private List<YxStoreProduct> getProductByRetailList(YxStoreProductQueryCriteria criteria) {
        return null;
    }
    //查询门店的库存中列表


}
